package com.xm;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Scanner;

/**
 * @Author：xm
 * @Project：runoob
 * @Date：2024/7/6 8:16
 */

// 使用 BufferedReader 在控制台读取字符
public class StreamAndFileAndIO03 {
    public static void main(String[] args) throws IOException {
        char c;
        String str;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("输入字符，按下‘q’键退出。");

        do {
            c = (char) br.read();
            System.out.println(c);
        } while (c != 'q');

        /*do {
            str = br.readLine();
            System.out.println("str = " + str);
        } while (!str.equals("end"));*/

        // runoob目录下需要存在test.txt文件
        FileOutputStream stream = new FileOutputStream("test.txt");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(stream);
        outputStreamWriter.write("你好");
        outputStreamWriter.flush();
        outputStreamWriter.close();

        System.out.println("Charset.defaultCharset().displayName() = " + Charset.defaultCharset().displayName());

        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("test.txt"));
        int len;
        // if ((len=inputStreamReader.read())!=-1){
        //     System.out.println("inputStreamReader.read() = " + (char)len);
        // }

        char[] chars = new char[1024];
        if ((len = inputStreamReader.read(chars)) != -1) {
            System.out.println(new String(chars,0,len));
        }
        inputStreamReader.close();

    }
}
